package com.iflytek.autofly.setting.biz.system;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.iflytek.autofly.setting.R;
import com.iflytek.autofly.setting.biz.MainActivity;
import com.iflytek.autofly.setting.biz.Notice;
import com.iflytek.autofly.utils.FlyLog;
import com.iflytek.sunflower.FlowerCollector;

/**
 * Created by admin on 2015/12/24.
 */
public class SysUpView implements View.OnClickListener {

    private static final java.lang.String TAG = "SysUpView";

    private LinearLayout mWhatsNewLl;
    private TextView mStateTv;
    private TextView mValueTv;
    private Button mActionBtn;
    private RelativeLayout mActionRl;
    private TextView mWhatsNewTv;

    // 控制器
    private SystemUpdater mSysUpdater;
    private MainActivity mActivity;
    private String mState;

    private SystemUpdater.OnSystemUpdaterChanged mOnSystemUpdaterChanged = new SystemUpdater.OnSystemUpdaterChanged() {
        @Override
        public void onSystemUpdateStateChanged(int state) {
            FlyLog.d(TAG, "state=" + state);
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    showSystemUpdate();
                }
            });
        }

        @Override
        public void onSystemUpdateProgressChanged(int progress) {
            final int curProgress = progress;
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mActionRl.setVisibility(View.VISIBLE);
                    mState = String.format(mActivity.getString(R.string.sys_sysup_state_downloading), curProgress);
                    FlyLog.d(TAG, mState);
                    mStateTv.setText(mState);
                    mActionBtn.setVisibility(View.VISIBLE);
                    mActionBtn.setText(mActivity.getString(R.string.sys_sysup_action_downloading));
                    mWhatsNewTv.setText(mActivity.getString(R.string.sys_sysup_whatsnew));
                    mWhatsNewLl.setVisibility(View.VISIBLE);
                    mValueTv.setText(mState);
                }
            });
        }

    };

    public void init(final Activity activity, final View v) {
        mActivity = (MainActivity) activity;
        mActionBtn = (Button) v.findViewById(R.id.sys_sysup_action_btn);
        mValueTv = (TextView) v.findViewById(R.id.sys_sysupdate_value_tv);
        mStateTv = (TextView) v.findViewById(R.id.sys_su_sys_state_tv);
        mActionRl = (RelativeLayout) v.findViewById(R.id.sys_sysup_action_rl);
        mWhatsNewLl = (LinearLayout) v.findViewById(R.id.sys_sysup_whatsnew_ll);
        mWhatsNewTv = (TextView) v.findViewById(R.id.sys_sysup_whatsnew_tv);
        mActionBtn.setOnClickListener(this);
        mWhatsNewLl.setOnClickListener(this);

        mSysUpdater = SystemUpdater.getInstance();
        mSysUpdater.init(mActivity, mOnSystemUpdaterChanged);

    }

    public void fini() {
        if (mSysUpdater != null) {
            mSysUpdater.fini();
            mSysUpdater = null;
        }
    }

    // 系统更新相关ui
    private void showSystemUpdate() {
        mActionBtn.setEnabled(true);
        switch (mSysUpdater.getSystemUpdateState()) {
            case SystemUpdater.SYS_UPDATE_STATE.INIT:
                AboutSystem as = AboutSystem.getInstance();
                as.init(mActivity);
                mState = String.format(mActivity.getString(R.string.sys_sysup_state_init), as.getSoftwareVersion());
                mActionBtn.setText(mActivity.getString(R.string.sys_sysup_action_init));
                mActionBtn.setVisibility(View.VISIBLE);
                mStateTv.setText(mState);
                mActionRl.setVisibility(View.VISIBLE);
                mWhatsNewLl.setVisibility(View.GONE);
                break;
            case SystemUpdater.SYS_UPDATE_STATE.CHECKING:
                mState = mActivity.getString(R.string.sys_sysup_state_checking);
                mActionBtn.setVisibility(View.GONE);
                mStateTv.setText(mState);
                mValueTv.setText(mState);
                mActionRl.setVisibility(View.VISIBLE);
                mWhatsNewLl.setVisibility(View.GONE);
                break;
            case SystemUpdater.SYS_UPDATE_STATE.CHECK_FAILED:
                mState = mActivity.getString(R.string.sys_sysup_state_check_failed);
                mActionBtn.setText(mActivity.getString(R.string.sys_sysup_action_check_failed));
                mActionBtn.setVisibility(View.VISIBLE);
                mStateTv.setText(mState);
                mValueTv.setText(mState);
                mActionRl.setVisibility(View.VISIBLE);
                mWhatsNewLl.setVisibility(View.GONE);
                break;
            case SystemUpdater.SYS_UPDATE_STATE.CHECKED:
                // 获取更新包大小
                long size = mSysUpdater.getSystemUpdateDataSize();
                if (size == 0) {
                    mActionRl.setVisibility(View.GONE);
                    mState = mActivity.getString(R.string.sys_sysup_state_no_update);
                    mWhatsNewTv.setText(mActivity.getString(R.string.sys_sysup_no_update));
                    mWhatsNewLl.setVisibility(View.VISIBLE);
                    mValueTv.setText(mState);
                } else {
                    mState = mActivity.getString(R.string.sys_sysup_state_new_version);
                    mActionBtn.setText(mActivity.getString(R.string.sys_sysup_action_new_version));
                    mWhatsNewTv.setText(mActivity.getString(R.string.sys_sysup_whatsnew));
                    mActionBtn.setVisibility(View.VISIBLE);
                    mWhatsNewLl.setVisibility(View.VISIBLE);
                    mStateTv.setText(mState);
                    mValueTv.setText(mState);
                    mActionRl.setVisibility(View.VISIBLE);
                }
                break;
            case SystemUpdater.SYS_UPDATE_STATE.DOWNLOADING:
                // 获取当前进度
                int progress = mSysUpdater.getSystemUpdateProgress();
                mState = String.format(mActivity.getString(R.string.sys_sysup_state_downloading), progress);
                mStateTv.setText(mState);
                mActionBtn.setVisibility(View.VISIBLE);
                mActionBtn.setText(mActivity.getString(R.string.sys_sysup_action_downloading));
                mWhatsNewTv.setText(mActivity.getString(R.string.sys_sysup_whatsnew));
                mWhatsNewLl.setVisibility(View.VISIBLE);
                mValueTv.setText(mState);
                mActionRl.setVisibility(View.VISIBLE);
                break;
            case SystemUpdater.SYS_UPDATE_STATE.DOWNLOAD_FAILED:
                mState = mActivity.getString(R.string.sys_sysup_state_download_failed);
                mStateTv.setText(mState);
                mActionBtn.setVisibility(View.VISIBLE);
                mActionBtn.setText(mActivity.getString(R.string.sys_sysup_action_download_failed));
                mWhatsNewTv.setText(mActivity.getString(R.string.sys_sysup_whatsnew));
                mWhatsNewLl.setVisibility(View.VISIBLE);
                mValueTv.setText(mState);
                mActionRl.setVisibility(View.VISIBLE);
                break;
            case SystemUpdater.SYS_UPDATE_STATE.DOWNLOAD_PAUSED:
                // 获取当前进度
                int progress2 = mSysUpdater.getSystemUpdateProgress();
                mState = String.format(mActivity.getString(R.string.sys_sysup_state_download_paused), progress2);
                mStateTv.setText(mState);
                mActionBtn.setVisibility(View.VISIBLE);
                mActionBtn.setText(mActivity.getString(R.string.sys_sysup_action_download_paused));
                mWhatsNewTv.setText(mActivity.getString(R.string.sys_sysup_whatsnew));
                mWhatsNewLl.setVisibility(View.VISIBLE);
                mValueTv.setText(mState);
                mActionRl.setVisibility(View.VISIBLE);
                break;

            case SystemUpdater.SYS_UPDATE_STATE.DOWNLOADED:
                mState = mActivity.getString(R.string.sys_sysup_state_download_complete);
                mStateTv.setText(mState);
                mActionBtn.setVisibility(View.VISIBLE);
                mActionBtn.setText(mActivity.getString(R.string.sys_sysup_action_download_complete));
                mWhatsNewTv.setText(mActivity.getString(R.string.sys_sysup_whatsnew));
                mWhatsNewLl.setVisibility(View.VISIBLE);
                mValueTv.setText(mState);
                mActionRl.setVisibility(View.VISIBLE);
                break;
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sys_sysup_action_btn:
                onActionButtonClick();
                break;
            case R.id.sys_sysup_whatsnew_ll:
                int state = mSysUpdater.getSystemUpdateState();
                switch (state) {
                    case SystemUpdater.SYS_UPDATE_STATE.INIT:
                    case SystemUpdater.SYS_UPDATE_STATE.CHECKING:
                    case SystemUpdater.SYS_UPDATE_STATE.CHECK_FAILED:
                        break;
                    case SystemUpdater.SYS_UPDATE_STATE.CHECKED:
                    case SystemUpdater.SYS_UPDATE_STATE.DOWNLOAD_FAILED:
                    case SystemUpdater.SYS_UPDATE_STATE.DOWNLOAD_PAUSED:
                    case SystemUpdater.SYS_UPDATE_STATE.DOWNLOADED:
                    case SystemUpdater.SYS_UPDATE_STATE.DOWNLOADING:
                        if (mSysUpdater.getSystemUpdateDataSize() != 0) {
                            FlowerCollector.onEvent(mActivity.getApplicationContext(), mActivity.getString(R.string.system) + "-" + mActivity.getString(R.string.sys_systemupdate) + "-" + mActivity.getString(R.string.sys_sysup_whatsnew), "" + state);
                            mActivity.showMassiveMessage(mActivity.getString(R.string.sys_sysup_whatsnew_title), mSysUpdater.getSystemUpdateInfo());
                        }
                        break;
                }
                break;
        }
    }

    private void onActionButtonClick() {
        int state = mSysUpdater.getSystemUpdateState();
        FlyLog.d(TAG, "state=" + state);
        FlowerCollector.onEvent(mActivity.getApplicationContext(), mActivity.getString(R.string.system) + "-" + mActivity.getString(R.string.sys_systemupdate) + "-" + mActionBtn.getText());
        switch (state) {
            case SystemUpdater.SYS_UPDATE_STATE.INIT:
                mSysUpdater.startCheckSystemUpdate();
                break;
            case SystemUpdater.SYS_UPDATE_STATE.CHECKING:
                break;
            case SystemUpdater.SYS_UPDATE_STATE.CHECK_FAILED:
                mSysUpdater.startCheckSystemUpdate();
                break;
            case SystemUpdater.SYS_UPDATE_STATE.CHECKED:
            case SystemUpdater.SYS_UPDATE_STATE.DOWNLOAD_FAILED:
            case SystemUpdater.SYS_UPDATE_STATE.DOWNLOAD_PAUSED:
                mSysUpdater.startDownloadSystemUpdate();
                break;
            case SystemUpdater.SYS_UPDATE_STATE.DOWNLOADING:
                mSysUpdater.pause();
                break;
            case SystemUpdater.SYS_UPDATE_STATE.DOWNLOADED:
                Intent intent = new Intent("iflytek.intent.action.UPGRADE_SYSTEM");
                intent.putExtra("ota_pkg", mSysUpdater.getPkgPath());
                Notice.getInstatnce().showWithBroadcast(mActivity.getString(R.string.sys_sysup_warn_msg), false, intent);
                break;
        }
    }
}
